//
//  GXSpecialCommentTableViewCell.m
//  GxLanOuProject
//
//  Created by 甘霞 on 15/10/28.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXSpecialCommentTableViewCell.h"
#import "NSString+GXSizeWithText.h"
#import "GXSuggestModel.h"
#import "UIImageView+WebCache.h"
#import "DailyViewCellHeader.h"
//#import "FontSizeManager.h"
#import "UIImage+GIF.h"

@interface GXSpecialCommentTableViewCell ()

@property (nonatomic, retain) UIImageView *mainImageView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *subTitleLabel;

@property (nonatomic, retain) UIImageView *playImageView;
@property (nonatomic, retain) UIImageView *typeImageView;
@property (nonatomic, retain) UILabel *timeLabel;
@property (nonatomic, retain) UIImageView *authorImageView;
@property (nonatomic, retain) UILabel *authorLabel;
@property (nonatomic, retain) UIImageView *quotesImageView;
@property (nonatomic, retain) UILabel *introLabel;
@property (nonatomic, retain) UILabel *imageCountLabel; 

@end

@implementation GXSpecialCommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeModel) name:@"changeModel" object:nil];
    
    //大图片
    self.mainImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.mainImageView];
    
    //播放
    self.playImageView = [[UIImageView alloc] init];
    [self.mainImageView addSubview:self.playImageView];
    [self.playImageView setImage:[UIImage imageNamed:@"btn-musicplay-play-active"]];
    [self.mainImageView addSubview:self.playImageView];
    
    //类型
    self.typeImageView = [[UIImageView alloc] init];
    [self.mainImageView addSubview:self.typeImageView];
    
    //时间
    self.timeLabel = [[UILabel alloc] init];
    [self.mainImageView addSubview:self.timeLabel];
    [self.timeLabel setTextColor:[UIColor whiteColor]];
    [self.timeLabel setBackgroundColor:[UIColor colorWithRed:219/255.0 green:219/255.0 blue:219/255.0 alpha:0.5]];
    [self.timeLabel setTextAlignment:NSTextAlignmentCenter];
    
    //作者头像
    self.authorImageView = [[UIImageView alloc] init];
    [self.mainImageView addSubview:self.authorImageView];
    self.authorImageView.layer.cornerRadius = 15;
    self.authorImageView.layer.masksToBounds = YES;
    
    //作者名字
    self.authorLabel = [[UILabel alloc] init];
    [self.mainImageView addSubview:self.authorLabel];
    [self.authorLabel setFont:[UIFont systemFontOfSize:kSubTitleLabelFontSize]];
    [self.authorLabel setTextColor:[UIColor whiteColor]];
    
    //引号
    self.quotesImageView = [[UIImageView alloc] init];
    [self.quotesImageView setImage:[UIImage imageNamed:@"icon-quote-black"]];
    [self.mainImageView addSubview:self.quotesImageView];
    
    //介绍
    self.introLabel = [[UILabel alloc] init];
    [self.mainImageView addSubview:self.introLabel];
    [self.introLabel setTextAlignment:NSTextAlignmentCenter];
    [self.introLabel setTextColor:[UIColor darkGrayColor]];
    [self.introLabel setFont:[UIFont systemFontOfSize:kIntroLabelFontSize]];
    [self.introLabel setNumberOfLines:0];
    
    //图片数量
    self.imageCountLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.imageCountLabel];
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    [self.titleLabel setFont:[UIFont systemFontOfSize:kTitleLabelFontSize]];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    [self.titleLabel setNumberOfLines:0];
    [self.contentView addSubview:self.titleLabel];
    
    //副标题
    self.subTitleLabel = [[UILabel alloc] init];
    [self.subTitleLabel setFont:[UIFont systemFontOfSize:kSubTitleLabelFontSize]];
    self.subTitleLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    [self.subTitleLabel setNumberOfLines:0];
    [self.subTitleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.subTitleLabel setTextColor:[UIColor grayColor]];
    [self.contentView addSubview:self.subTitleLabel];
}

- (void)changeModel {
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    self.subTitleLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //大图
    [self.mainImageView setFrame:CGRectMake(0, kStringBorder, kCellWidth, kMainViewHeight)];
    //播放
    [self.playImageView setFrame:CGRectMake(0, 0, 80, 80)];
    self.playImageView.center = self.mainImageView.center;
    //类型
    CGSize typeImageSize = self.typeImageView.image.size;
    [self.typeImageView setFrame:CGRectMake(kCellWidth - typeImageSize.width - kSpecialCommentBorder, kSpecialCommentBorder, typeImageSize.width, typeImageSize.height)];
    //时间
    [self.timeLabel setFrame:CGRectMake(0, kMainViewHeight - kTimeLabelHeight, kCellWidth, kTimeLabelHeight)];
    //作者头像
    [self.authorImageView setFrame:CGRectMake(kSpecialCommentBorder, kSpecialCommentBorder, kAuthorImageViewSize, kAuthorImageViewSize)];
    [self.authorImageView setBackgroundColor:[UIColor blackColor]];
    //作者名字
    CGFloat authorLabelX = CGRectGetMaxX(self.authorImageView.frame) + 10;
    [self.authorLabel setFrame:CGRectMake(authorLabelX, kSpecialCommentBorder, kCellWidth - 2 * kSpecialCommentBorder, kTimeLabelHeight)];
    
    //标题
    CGFloat labelW = kCellWidth - 2 * kSpecialCommentBorder;
    CGRect titleLabelSize = [self.titleLabel.text SizeWithTextFontSize:kTitleLabelFontSize textWidth:labelW];
    CGFloat titleLabelY = CGRectGetMaxY(self.mainImageView.frame) + kStringBorder;
    [self.titleLabel setNumberOfLines:0];
    [self.titleLabel setFrame:CGRectMake(kSpecialCommentBorder, titleLabelY, labelW, titleLabelSize.size.height)];
    
    //引号
    CGSize qouteImageSize = self.quotesImageView.image.size;
    CGFloat qouteImageY = CGRectGetMaxY(self.titleLabel.frame);
    [self.quotesImageView setFrame:CGRectMake(kCellWidth / 2.0 - qouteImageSize.width / 2.0, qouteImageY, qouteImageSize.width, qouteImageSize.height)];
    
    //简介
    CGFloat introLabelY = CGRectGetMaxY(self.quotesImageView.frame) + 20;
//    CGRect introlLabelSize = [self.introLabel.text SizeWithTextFontSize:kIntroLabelFontSize textWidth:labelW];
//    [self.introLabel setFrame:CGRectMake(kSpecialCommentBorder, introLabelY, labelW, introlLabelSize.size.height)];
    
    //副标题
    CGRect subTitleSize = [self.subTitleLabel.text SizeWithTextFontSize:kSubTitleLabelFontSize textWidth:labelW];
//    CGFloat subTitleLabelY = CGRectGetMaxY(self.introLabel.frame) + kSpecialCommentBorder;
    [self.subTitleLabel setFrame:CGRectMake(kSpecialCommentBorder, introLabelY, labelW, subTitleSize.size.height)];
//    [self.subTitleLabel setFrame:CGRectMake(kSpecialCommentBorder, subTitleLabelY, labelW, subTitleSize.size.height)];
}

- (void)setSuggestModel:(GXSuggestModel *)suggestModel {

    if (_suggestModel != suggestModel ) {
        _suggestModel = suggestModel;
    }
    
    //文章
    if (suggestModel.type.integerValue == 0) {
        self.playImageView.hidden = YES;
        self.typeImageView.hidden = YES;
        self.timeLabel.hidden = YES;
        if (suggestModel.has_video == true) {
            self.playImageView.hidden = NO;
        }
    }
    //视频
    else if (suggestModel.type.integerValue == 2) {
        self.typeImageView.hidden = YES;
        self.timeLabel.hidden = YES;
        self.introLabel.text = _suggestModel.intro;
    }
    //音乐
    else if (suggestModel.type.integerValue == 10) {
        self.playImageView.hidden = NO;
        self.typeImageView.image = [UIImage imageNamed:@"icon-music"];
        NSNumber *musicTime = _suggestModel.music[@"music_duration"];
        self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", musicTime.integerValue / 60, musicTime.integerValue % 60];
        self.introLabel.text = _suggestModel.intro;
    }

    //图片
    [self.mainImageView sd_setImageWithURL:_suggestModel.thumb[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //作者头像
    [self.authorImageView sd_setImageWithURL:_suggestModel.cp[@"avatar"]];
    //作者名字
    [self.authorLabel setText:_suggestModel.cp[@"screen_name"]];
    //标题
    [self.titleLabel setText:_suggestModel.title];
    //副标题
    [self.subTitleLabel setText:_suggestModel.content[@"desc"]];
}

@end
